package com.example.yqand4v1_1.controller;

import com.example.yqand4v1_1.beans.Result;
import com.example.yqand4v1_1.common.utils.CurPool;
import com.example.yqand4v1_1.common.utils.JwtUtil;
import com.example.yqand4v1_1.dao.entity.Volunteer;
import com.example.yqand4v1_1.service.VolunteerService;
import com.nimbusds.jose.JOSEException;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

@RestController
@RequestMapping("/permission")
@CrossOrigin
public class VolunteerController {
    @Resource
    private VolunteerService volunteerService;


    @RequestMapping(value = "/LoginInfo",method = RequestMethod.POST,headers = "Accept=application/json")
    public Result<Map<String,Object>> login(@RequestBody Volunteer volunteer){
        Map<String,Object> loginVolunteer = volunteerService.login(volunteer);
        //success
        if(loginVolunteer != null){
            String token = (String) loginVolunteer.get("token");
            loginVolunteer.remove("token");
            try {
                loginVolunteer.put("token", JwtUtil.createToken((String) loginVolunteer.get("name"),String.valueOf(loginVolunteer.get("id"))));
            } catch (JOSEException e) {
                throw new RuntimeException(e);
            }
            return Result.success(loginVolunteer,token);
        }else {
            return Result.fail(404,"用户名或密码错误");
        }

    }



    @RequestMapping(value = "/changeUserInfo",method = RequestMethod.POST,headers = "Accept=application/json")
    public Result<Map<String,Object>> changeUserInfo(@RequestBody Volunteer volunteer){
        Map<String,Object> changeUser = volunteerService.changeUserInfo(volunteer);
        //success
        if(changeUser != null){
            String token = (String) changeUser.get("token");
            changeUser.remove("token");
            return Result.success(changeUser,token);
        }else {
            return Result.fail(404,"修改失败");
        }

    }


    @RequestMapping(value = "/registerUser",method = RequestMethod.POST,headers = "Accept=application/json")
    public Result<Map<String,Object>> register(@RequestBody Volunteer volunteer){
        Map<String,Object> result = volunteerService.register(volunteer);
        if(result != null) {
            return Result.success(result);
        }else {
            return Result.fail(404,"注册失败");
        }
    }

    @RequestMapping(value = "/changePsw",method = RequestMethod.POST,headers = "Accept=application/json")
    public Result<Map<String,Object>> changePsw(@RequestBody Volunteer volunteer){
        Boolean result = volunteerService.changePsw(volunteer);
        if(result) {
            return Result.success();
        }else {
            return Result.fail(404,"修改失败");
        }
    }

    @RequestMapping(value = "/checkInfo",method = RequestMethod.POST,headers = "Accept=application/json")
    public Result<Map<String,Object>> checkInfo(@RequestBody Volunteer volunteer){
        Boolean result = volunteerService.checkInfo(volunteer);
        if(result) {
            return Result.success();
        }else {
            return Result.fail(404,"修改失败");
        }
    }
}
